// pages/my/orderlist/returndetail/returndetail.js
import { axios } from '../../../../utils/ajax.js'
import { gettime } from '../../../../utils/djs.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //默认收缩的物流信息高度
    loghideheight: '',
    initialheight: '',
    //快递公司选择
    companyList:[],
    companyindex:'',
    //显示的物流公司名字
    companyname:'',
    companyid:'',
    data:'',
    //退货进度状态
    status:'',
    //物流信息
    expressInfo:'',
    companyshow:false,
    maskstate: false,
    //显示的物流单号
    logid:'',
    returnNo: '',
    userid: '',
    token: '',
    //当前进度的序列号
    curindex: 0,
    //读取到的用户物流单号 判断是否可以直接输入
    disbtn:'',
    //倒计时时间戳
    submitTime:'',
    timeline: '',
    //退货描述
    nodes:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let returno = options.returno
    let { userid = '', token = '' } = wx.getStorageSync('userinfo')
    this.setData({
      returnNo: returno,
      userid: userid,
      token: token
    })

    //获取物流公司列表
    axios({
      url: "/returnGoods/expressCompany"
    }).then((res) => {
      console.log(res)
      this.setData({
        companyList: res.data.companyList
      })
    })


    //获取页面数据
    axios({
      url: '/returnGoods/detail',
      data: {
        returnNo: returno, 
        userid: userid,
        token: token
      }
    }).then((res) => {
      console.log(res)
      this.setData({
        data: res.data,
        status: res.data.status,
        expressInfo: res.data.expressInfo,
        logid: res.data.deliveryNo,
        disbtn: res.data.deliveryNo ? true:false,
        submitTime: res.data.submitTime ? res.data.submitTime:'',
        nodes: res.data.info
      })
      //倒计时
      this.setData({
        timeline: gettime(this.data.submitTime) ? '还剩'+gettime(this.data.submitTime):''
      })
      let subtimer = setInterval(()=>{
        if (gettime(this.data.submitTime)){
          this.setData({
            timeline: '还剩' +gettime(this.data.submitTime)
          })
        }else{
          clearInterval(subtimer)
          this.setData({
            timeline: ''
          })
        }
      },1000)

      //获取当前进度的序列号
      let status = this.data.status
      //如果有状态2
      if (status.find(x => x.activate == '2')){
        status.forEach((val, index) => {
          //如果有状态2
          if (val.activate == '2') {
            this.setData({
              curindex: index
            })
          }
        })
      //没2的话 找1里面最大的
      }else{
        let curindex = this.data.curindex
        status.forEach((val, index) => {
          if (val.activate == '1') {
            curindex = index
          }
        })
        this.setData({
          curindex: curindex
        })
      }

      console.log(this.data.curindex)
      if (this.data.expressInfo && this.data.expressInfo.length){
        //物流信息查询第一条高度  放在ajax内
        wx.createSelectorQuery().select('.lastestlog').boundingClientRect().exec(res => {
          this.setData({
            loghideheight: res[0].height,
            initialheight: res[0].height
          })
        })
      }
      //读取用户填写的物流信息
      let companyname = res.data.expressCompanyCode?this.data.companyList.find(x => x.compayId == res.data.expressCompanyCode).compayName:""
      this.setData({
        companyname: companyname
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //拨打退换货电话
  callreturn(e) {
    let tel = e.currentTarget.dataset.tel
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  //物流展开
  showlog () {
    this.setData({
      loghideheight: 'auto'
    })
  },
  //物流收缩
  hidelog() {
    this.setData({
      loghideheight: this.data.initialheight
    })
  },
  //选择物流公司
  choosecompany (e) {
    let companyid = e.currentTarget.dataset.companyid
    let index = e.currentTarget.dataset.index
    let companyname = e.currentTarget.dataset.companyname
    console.log(companyid,companyname,index)
    this.setData({
      companyindex: index,
      companyname: companyname,
      companyid: companyid
    })
  },
  //打开选择物流公司
  opencompany () {
    this.setData({
      companyshow: true,
      maskstate: true
    })
  },
  //关闭选择物流公司
  hidecompany(){
    this.setData({
      companyshow:false,
      maskstate: false
    })
  },
  //提交物流信息
  commitmsg () {
    let logid = this.data.logid
    if (!this.data.companyid){
      wx.showToast({
        title: '请选择物流公司',
        icon: "none"
      })
    } else if (!logid){
      wx.showToast({
        title: '请填写物流单号',
        icon: "none"
      })
    }else{
      axios({
        url: '/returnGoods/uploadExpressNo',
        data: {
          userid: this.data.userid,
          token: this.data.token,
          returnNo: this.data.returnNo,
          expressCompanyId: this.data.companyid,
          deliveryNo: logid
        }
      }).then((res) => {
        console.log(res)
        wx.showToast({
          title: res.data.message,
          icon:"none"
        })
      })
    }
  },
  //绑定物流单号输入内容
  inputlogid (e) {
    this.setData({
      logid: e.detail.value
    })
  },
  //修改物流
  changelog () {
    this.setData({
      disbtn: false
    })
  },
  //去商品详情页
  gogoodsdatail (e) {
    let goodsid = e.currentTarget.dataset.goodsid
    console.log(goodsid)
    wx.navigateTo({
      url: `../../../home/goodsDetail/goodsDetail?linkId=${goodsid}`,
    })
  } 
})